﻿using System.Text;

namespace MyHttp
{
    public class MyHttpClientHelper
    {
        /// <summary>
        /// Get请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <returns>返回请求结果</returns>
        public static async Task<string> HttpGet(string url)
        {
            try
            {
                using (var httpClient = new HttpClient())
                {
                    using (var response = await httpClient.GetAsync(url))
                    {
                        var result = await response.Content.ReadAsStringAsync();
                        return result;
                    }
                }
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="body">请求体</param>
        /// <returns>返回请求结果</returns>
        public static async Task<string> HttpPost(string url, string body)
        {
            try
            {
                using (var httpClient = new HttpClient())
                {
                    using (StringContent httpContent = new StringContent(body, Encoding.UTF8, "application/json"))
                    {
                        httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                        using (var response = await httpClient.PostAsync(url, httpContent))
                        {
                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                    };
                }
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Patch请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="body">请求体</param>
        /// <returns>返回请求结果</returns>
        public static async Task<string> HttpPatch(string url, string body)
        {
            try
            {
                using (var httpClient = new HttpClient())
                {
                    using (HttpContent httpContent = new StringContent(body))
                    {
                        using (var response = await httpClient.PatchAsync(url, httpContent))
                        {
                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                    }
                }
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Put请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="body">请求体</param>
        /// <returns>返回请求结果</returns>
        public static async Task<string> HttpPut(string url, string body)
        {
            try
            {
                using (var httpClient = new HttpClient())
                {
                    using (HttpContent httpContent = new StringContent(body))
                    {
                        using (var response = await httpClient.PutAsync(url, httpContent))
                        {
                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                    }
                }
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Delete请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <returns>返回请求结果</returns>
        public static async Task<string> HttpDelete(string url)
        {
            try
            {
                using (var httpClient = new HttpClient())
                {
                    using (var response = await httpClient.DeleteAsync(url))
                    {
                        var result = await response.Content.ReadAsStringAsync();
                        return result;
                    }
                }
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="body">请求体</param>
        /// <returns>返回请求结果</returns>
        public static async Task<string> HttpUploadFile(string url, string body)
        {
            try
            {
                using (var httpClient = new HttpClient())
                {
                    using (StringContent httpContent = new StringContent(body, Encoding.UTF8, "application/json"))
                    {
                        httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                        using (var response = await httpClient.PostAsync(url, httpContent))
                        {
                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                    };
                }
            }
            catch
            {
                throw;
            }
        }
    }

}
